package com.company.condition;

import com.company.bean.RainBow;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

public class MyImportBeanDefinitions implements ImportBeanDefinitionRegistrar {

    /*
    AnnotationMetadata 当前类的注解信息
    BeanDefinitionRegistry BeanDefinition注册类
         把所有需要添加到容器类的bean,BeanDefinitionRegistry.registerBeanDefinition 自定义手工注册

     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean definition = registry.containsBeanDefinition("red");
        boolean definition1 = registry.containsBeanDefinition("blue");
        if (definition && definition1) {
            //指定bean命
            RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
            registry.registerBeanDefinition("rainBow", beanDefinition);
        }
    }
}
